package com.jianyun.jyzs.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jianyun.jyzs.ThisApp;
import com.jianyun.jyzs.bean.Account;
import com.jianyun.jyzs.bean.MeUserInfo;
import com.jianyun.jyzs.bean.OAItem;
import com.jianyun.jyzs.bean.SignVersionBean;
import com.jianyun.jyzs.bean.UrlConfig;
import com.jianyun.jyzs.bean.UserInfo;
import com.jianyun.jyzs.constant.SysConstant;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by mlli on 2017/10/19.
 */

public class LoginCache {
    private Gson gson;
    private SharedPreferences preferences;
    private final static String LOGIN_CACHE = "login_cache";
    private final String login_response = "LOGIN_RESPONSE";
    private static final String LOGIN_RESPONSE = "login_response";
    private static final String LOGIN_TAG = "login_tag";//是否登录成功
    private static final String LOGIN_INFO = "login_info";
    private static final String LOGIN_ENABlE = "login_enable";//登录后的权限
    private static final String USER_INFO = "user_info";
    private static final String USER_DETAIL_INFO = "user_detaile_info";
    private static final String WORKFLOW_ID = "workflow_id";
    private static final String NOTICE_ID = "notice_id";
    private static final String UPDATE_TIME = "update_time";
    private static final String SIGN_VERSION = "sign_version";
    private static final String FLOW_ISENABLE="flow_isenable";

    private LoginCache() {
        init();
    }

    private void init() {
        gson = new Gson();
        preferences = ThisApp.applicationContext.getSharedPreferences(LOGIN_CACHE, Context.MODE_PRIVATE);

    }

    private static class SingleTonHolder {
        private static LoginCache sIns = new LoginCache();
    }

    public static LoginCache getInstance() {
        return SingleTonHolder.sIns;
    }

    public void setLoginCache(UrlConfig response) {
        //String enable = response.getEnable();
        //setEnablelogin(enable);
        String s = gson.toJson(response);
        preferences.edit().putString(LOGIN_RESPONSE, s).apply();
    }

    public void setEnablelogin(String enable) {
//        int length = enable.length();
//        char[] chars = enable.toCharArray();
        preferences.edit().putString(LOGIN_ENABlE, enable).apply();
    }

    public String getEnableLogin() {
        String enbale = preferences.getString(LOGIN_ENABlE, "");
        return enbale;
    }


    public UrlConfig getLoginCache() {
        String s = preferences.getString(LOGIN_RESPONSE, null);
        if (s != null) {
            UrlConfig urlConfig = gson.fromJson(s, UrlConfig.class);
            return urlConfig;
        } else {
            return null;
        }
    }

    public List<Integer> getMenuPoint() {
        List<Integer> menuPoint = new ArrayList<>();
        List<OAItem> menuMessageVo = getMenuMessageVo();
        for (OAItem mv : menuMessageVo) {
//            menuPoint.add(mv.count);
        }
        return menuPoint;
    }

    public List<OAItem> getMenuMessageVo() {
        String string = preferences.getString(SysConstant.menu_messagevo, null);
        List<OAItem> list = new ArrayList<>();
        if (string != null) {
            list = gson.fromJson(string, new TypeToken<List<OAItem>>() {
            }.getType());
            return list;
        } else {
            OAItem messageVO = null;
//            for (int i = 0 ; i <SysConstant.menu_title.length;i++){
//                messageVO = new OAItem();
//                messageVO.count = 0;
//                messageVO.isShow = false;
//                messageVO.context = SysConstant.menu_title[i];
//                messageVO.img = SysConstant.menu_list[i];
////                if (i==SysConstant.menu_list.length-1)messageVO.commingsoon= true;
//                list.add(messageVO);
//            }
            return list;
        }
    }

    public List<Integer> getTopPoint() {
        List<Integer> topPoint = new ArrayList<>();
        List<OAItem> topMessageVo = getTopMessageVo();
        for (OAItem mv : topMessageVo) {
//            topPoint.add(mv.count);
        }
        return topPoint;
    }

    public List<OAItem> getTopMessageVo() {
        String string = preferences.getString(SysConstant.tp_messagevo, null);
        List<OAItem> list = new ArrayList<>();
        if (string != null) {
            list = gson.fromJson(string, new TypeToken<List<OAItem>>() {
            }.getType());
            return list;
        } else {
            OAItem messageVO = null;
//            for (int i = 0 ;i<SysConstant.top_title.length;i++) {
//                messageVO = new OAItem();
//                messageVO.count = 0;
//                messageVO.isShow = false;
//                messageVO.context = SysConstant.top_title[i];
//                messageVO.img = SysConstant.img_list[i];
//                list.add(messageVO);
//            }
            return list;
        }

    }

    public boolean isCacheValid() {
        return preferences.getBoolean(LOGIN_TAG, false);
    }

    public void setLoginTure(boolean login) {
        preferences.edit().putBoolean(LOGIN_TAG, login).apply();
    }

    public void setLoginOut() {
        preferences.edit().putBoolean(LOGIN_TAG, false).apply();
    }

    public void setLoginInfo(Account loginHistory) {
        String s = gson.toJson(loginHistory);
        preferences.edit().putString(LOGIN_INFO, s).apply();
    }

    public Account getLoginInfo() {
        String s = preferences.getString(LOGIN_INFO, null);
        if (s != null) {
            return gson.fromJson(s, Account.class);
        } else {
            return null;
        }
    }

    /**
     * 登录成功后保存 用户信息
     *
     * @param userId
     * @param username
     * @param password
     */
    public void setUserInfo(String username, String password, String userId) {
        UserInfo userInfo = new UserInfo(username, password, userId);
        String json = gson.toJson(userInfo);
        preferences.edit().putString(USER_INFO, json).apply();

    }

    public UserInfo getUserInfo() {
        String s = preferences.getString(USER_INFO, null);
        if (s != null) {
            return gson.fromJson(s, UserInfo.class);
        } else {
            return null;
        }
    }

    /**
     * 登录成功后保存 用户详细信息
     */
    public void setUserDetaileInfo(MeUserInfo meUserInfo) {
        String json = gson.toJson(meUserInfo);
        preferences.edit().putString(USER_DETAIL_INFO, json).apply();

    }

    public MeUserInfo getUserDetaileInfo() {
        String s = preferences.getString(USER_DETAIL_INFO, null);
        if (s != null) {
            return gson.fromJson(s, MeUserInfo.class);
        } else {
            return null;
        }
    }

    public void setSignVersion(SignVersionBean signVersionBean) {
        String s = gson.toJson(signVersionBean);
        preferences.edit().putString(SIGN_VERSION, s).apply();
    }

    public SignVersionBean getSignVersion() {
        String s = preferences.getString(SIGN_VERSION, null);
        if (s != null) {
            return gson.fromJson(s, SignVersionBean.class);
        } else {
            return null;
        }
    }

    /**
     * 流程审批分类
     * @param swtich
     */
    public void setFlowSwtich(int swtich){
        preferences.edit().putInt(FLOW_ISENABLE,swtich).apply();
    }

    public int getFlowSwtich(){
        return preferences.getInt(FLOW_ISENABLE,-1);
    }
}
